<?php
// 需要预定义 UNIQID (高并发时区分不同客户端的请求, 跟踪id)
class FileLog
{
    private $dir = ''; //disk_free_space()
    private $middleDir = ''; //中间目录
    private $file = ''; //文件名
    private $filePath = ''; //日志文件完整路径
    private $separator = "\t";
    public $uuid = ''; //追踪用唯一字符串

    /**
     * @param array|string $dir 要写入的目录
     * @param string $file 文件名, 默认为当天的日期
     * @return $this
     * @throws Exception
     */
    public static function ini($dir='', $file='')
    {
        $obj = new FileLog();

        //组装目录
        if (is_array($dir)) {
            $obj->middleDir = implode('/', $dir);
        } else {
            $obj->middleDir = $dir;
        }

        $prefix = rtrim(LOGPATH, '/');
        $middle = trim($obj->middleDir, '/');

        if (!empty($middle)) {
            $obj->filePath = $prefix.'/'.$middle.'/';
        } else {
            $obj->filePath = $prefix.'/';
        }


        //创建目录
        if (file_exists($obj->filePath) === FALSE) {
            if (!mkdir($obj->filePath, 0766, true)) {
                throw new Exception('创建日志目录失败: '.$obj->filePath);
            }
        }

        //组装文件
        $obj->file = empty($file) ? REQUEST_DAY : $file;
        $obj->filePath .= $obj->file . '.log';

        //日志跟踪编号
        $obj->uuid = UNIQID;

        return $obj;
    }

    public function __construct()
    {}
    
    /**
     * 设置日志信息的分隔符, 不设置则用默认值
     * @param $str
     * @return $this
     */
    public function setSeparator($str)
    {
        $this->separator = $str;
        return $this;
    }

    public function setUUID($uuid = '')
    {
        $this->uuid = empty($uuid) ? UNIQID : $uuid;
        return $this;
    }
    
    /**
     * 记录日志
     * @param string|array|object $msg 日志信息
     * @param string $prefix 说明性文字 (单独列出来是为了, 不影响对数据信息的json_encode())
     * @param string $level 错误级别
     * @return $this
     */
    private function log($msg, $prefix='', $level='')
    {
        if (is_array($msg) || is_object($msg)) {
            //$msg = json_encode($msg, JSON_UNESCAPED_UNICODE); //汉字原样显示, 不显示成编码格式
            $msg = json_encode($msg);
        }

        $this->uuid = empty($this->uuid) ? UNIQID : $this->uuid;

        $datetime = date('Y-m-d H:i:s');
        $microTime = explode('.', microtime(true));
        $microSec = end($microTime);
        $arr = [$datetime.'.'.$microSec, $this->uuid, $level, $prefix, $msg];
        $string = implode($this->separator, $arr).PHP_EOL;
        file_put_contents($this->filePath, $string, FILE_APPEND ); //追加模式

        return $this;
    }

    public function debug($msg, $prefix='')
    {
        if (LOG_LEVEL >= 1) {
            return $this->log($msg, $prefix, 'debug');
        }
        return $this;
    }

    public function info($msg, $prefix='')
    {
        if (LOG_LEVEL >= 2) {
            return $this->log($msg, $prefix, 'info');
        }
        return $this;
    }

    public function notice($msg, $prefix='')
    {
        if (LOG_LEVEL >= 3) {
            return $this->log($msg, $prefix, 'notice');
        }
        return $this;
    }

    public function warning($msg, $prefix='')
    {
        if (LOG_LEVEL >= 4) {
            return $this->log($msg, $prefix, 'warning');
        }
        return $this;
    }

    public function error($msg, $prefix='')
    {
        if (LOG_LEVEL >= 5) {
            return $this->log($msg, $prefix, 'error');
        }
        return $this;
    }

}
